home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.03 Mar 90 / XCMD Mar Code / •Listing 3•MiniParser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-12-06  |  4.5 KB  |  222 lines  |  [TEXT/KAHL]

  1. /********************************/
  2. /* File: ParseCommands.c        */
  3. /*                                */
  4. /* Parse commands coming in from*/
  5. /* Hypercard.                    */
  6. /*                                */
  7. /* Once the object is parsed, it*/
  8. /* is added to the draw list for*/
  9. /* the window.                    */
  10. /*                                 */
  11. /* Eventually need to match     */
  12. /* multiple tokens so that we     */
  13. /* can have parameters such as:    */
  14. /* BOLD&ITALIC&OUTLINE            */
  15. /* ----------------------------    */
  16. /* © 1989 Donald Koscheka        */
  17. /* All Rights Reserved            */
  18. /********************************/
  19.  
  20. #define        CR                    0x0D
  21. #define        NEWLINE                0x0D
  22. #define        LF                    0x0A
  23. #define        TAB                    0x09
  24. #define        ETX                    0x03    /* the enter key */
  25.  
  26. long matchToken( buf, tabl )
  27.     Handle    buf;
  28.     short    tabl;
  29. /*******************************
  30. * given an input buffer and the resource 
  31. * id of the parse table, return 
  32. * the token that represents the input
  33. * string.
  34. *
  35. * A token of 0 is returned if no
  36. * match is found.  This way, you
  37. * can use the first item in the list
  38. * as the default item!
  39. *
  40. * The symbol table should have the 
  41. * format:
  42. *
  43. *    <string>, <token>
  44. * where string mathces to the input
  45. * string and token is that value for 
  46. * a given match.
  47. *******************************/
  48. {
  49.     char    *bp;            /*** pointer to input strings            ***/
  50.     Handle    strH;            /*** handle to parse strings resource    ***/
  51.     long    token     = 0;    /*** return the default if no match        ***/
  52.     short    indx    = 0;
  53.     short    theID;
  54.     ResType    theType;    
  55.     short    done = 0;
  56.     long    len;
  57.     char    theNum[31];    
  58.     char    *np;
  59.     char    theName[256];
  60.     char    theString[256];
  61.         
  62.     bp = *buf;
  63.     
  64.     while( *bp ){
  65.         toUpper( *bp );
  66.         bp++;
  67.     }
  68.     
  69.     strH     = GetResource( 'STR#', tabl );
  70.     
  71.     if( strH ){
  72.         
  73.         GetResInfo( strH, &theID, &theType, &theName );
  74.         
  75.         /*** compare the string to the allowable tokens ***/
  76.         indx    = 1 ;
  77.         
  78.         while( !done ){
  79.             theString[0] = '\0';
  80.             GetIndString( &theString, tabl, indx );
  81.             
  82.             if( theString[0] == '\0' ){    /* no strings matched the input     */
  83.                 done = 1;
  84.             }
  85.             else{                        /* attempt to match to current str    */
  86.                 
  87.                 PtoCstr( (char *)&theString );
  88.                 
  89.                 len = 0;
  90.                 
  91.                 bp = theString;
  92.                 while ( *bp != ',' ){
  93.                     bp++;
  94.                     len++;
  95.                 }
  96.             
  97.                 if( strncmp( *buf, (char *)theString, len ) == 0){
  98.                     /* have a match so extract the token    */                    
  99.                     
  100.                     /*** move past any garbage in the string ***/
  101.                     while( (*bp < '0' || *bp > '9') && *bp != '-')
  102.                         bp++;
  103.                     
  104.                     /*** now copy what bp points to into a     ***/
  105.                     /*** a pascal style string                ***/
  106.                     
  107.                     theNum[0] = '\0';
  108.                     np = &theNum[1];
  109.                     
  110.                     while( *bp >= '0' && *bp <= '9' ){
  111.                         theNum[0]++;
  112.                         *np++ = *bp++;
  113.                     }
  114.                     
  115.                     /*** np is a valid p-string                ***/
  116.                     StringToNum( theNum, &token );
  117.                     done = 1;
  118.                 }
  119.                 else
  120.                     indx++;
  121.             }
  122.         }
  123.     }
  124.     
  125.     return( token );
  126. }
  127.  
  128.  
  129. long    parseNum( bp )
  130.             char    *bp;
  131. /***********************
  132. * parse the data stream 
  133. * and return a numeric 
  134. * value.  The stream is null
  135. * terminated.
  136. *
  137. ***********************/
  138. {
  139.     long    num = 0;
  140.     
  141.     char    theString[256];
  142.     short    done = 0;
  143.     char    *ps;
  144.         
  145.     /*** move the input pointer until we're looking at a number    ***/
  146.     while( *bp && ( *bp < '0' || *bp > '9' ) && *bp != '-' )
  147.         bp++;
  148.     
  149.     /*** copy the data into a pascal string     ***/
  150.     ps    = theString;
  151.     ps++;
  152.     
  153.     while( *bp >= '0' && *bp <= '9' )
  154.         *ps++ = *bp++;
  155.     
  156.     /*** moved one past the output so try this    ***/
  157.     theString[0] = (char)( ps - theString -1  );    
  158.     StringToNum( theString, &num );
  159.     
  160.     return( num );    
  161. }
  162.  
  163. char    *nextToken( bp )
  164.             char    *bp;
  165. /***********************
  166. * given a pointer to an
  167. * input stream, move to the 
  168. * next token in the stream. 
  169. * Token's are delineated by
  170. * ',' or whitespace
  171. *
  172. * Assumes we are pointing to the current token
  173. * Move past the current token and the white
  174. * space that follows it.
  175. ***********************/
  176. {
  177.     /*** move past the current token    ***/
  178.     while( *bp &&(*bp != ',' && *bp != SPACE && *bp != CR && *bp != LF && *bp != TAB) )
  179.         bp++;
  180.  
  181.     /*** move past the white space to the next token    ***/
  182.     while( *bp == ',' || *bp == SPACE || *bp == CR || *bp == LF || *bp == TAB )
  183.         bp++;
  184.     
  185.     return( bp );
  186. }
  187.  
  188. void     parseRect( buf, theRect )
  189.             Handle    buf;
  190.             Rect    *theRect;
  191. /***********************
  192. * parse the data stream 
  193. * into a rectangle
  194. *
  195. * default is the NULL rect
  196. ***********************/
  197. {
  198.     char    *bp;
  199.     
  200.     theRect->top = theRect->left = theRect->bottom = theRect->right = 0;
  201.     
  202.     if( validHandle( buf ) ){
  203.         HLock( buf );
  204.         
  205.         bp = *buf;
  206.         theRect->top = parseNum( bp );
  207.         bp    = nextToken( bp );
  208.         
  209.         theRect->left = parseNum( bp );
  210.         bp    = nextToken( bp );
  211.         
  212.         theRect->bottom = parseNum( bp );
  213.         bp    = nextToken( bp );
  214.         
  215.         theRect->right = parseNum( bp );
  216.         bp    = nextToken( bp );
  217.         
  218.         HUnlock( buf );
  219.     }
  220. }